package com.living.filedownloader.ui.batch;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.living.R;
import com.living.filedownloader.bean.TasksManagerModel;
import com.living.filedownloader.ui.adapter.TaskItemDownloadedSelectAdapter;
import com.md.basedpc.base.BaseFragment;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class AudioBatchFragment extends BaseFragment implements OnItemClickListener, View.OnClickListener {

    TextView tv_batch_left, tv_batch_title, tv_batch_right;
    View layout_edite_tip;
    TextView tv_edite_tip;

    private Map<String, TasksManagerModel> mapSel = new ConcurrentHashMap<>();
    private TaskItemDownloadedSelectAdapter taskItemAdapter;
    private ArrayList<TasksManagerModel> downloadedList = new ArrayList<>();

    @Override
    protected int getLayout() {
        return R.layout.fragment_download_batch_delete;
    }

    public Map<String, TasksManagerModel> getMapSel() {
        return mapSel;
    }

    /**
     * 移除操作
     *
     * @param t
     */
    public void remove(TasksManagerModel t) {
        taskItemAdapter.remove(t);
        downloadedList.remove(t);
        refreshSelect();
        if (taskItemAdapter.getData().size() == 0) {
            getActivity().finish();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        tv_batch_left = view.findViewById(R.id.tv_batch_left);
        tv_batch_title = view.findViewById(R.id.tv_batch_title);
        tv_batch_right = view.findViewById(R.id.tv_batch_right);

        layout_edite_tip = view.findViewById(R.id.layout_edite_tip);
        tv_edite_tip = view.findViewById(R.id.tv_edite_tip);

        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        downloadedList = (ArrayList<TasksManagerModel>) getActivity().getIntent().getSerializableExtra("list");

        taskItemAdapter = new TaskItemDownloadedSelectAdapter(mapSel);
        recyclerView.setAdapter(taskItemAdapter);

        taskItemAdapter.setOnItemClickListener(this);
        tv_batch_left.setOnClickListener(this);
        tv_batch_right.setOnClickListener(this);

        taskItemAdapter.setNewInstance(downloadedList);
        tv_edite_tip.setText("共" + downloadedList.size() + "首");
        refreshSelect();

        return view;
    }

    private void refreshSelect() {
        if (mapSel.size() == downloadedList.size()) {
            tv_batch_left.setText("取消全选");
        } else {
            tv_batch_left.setText("全选");
        }
        if (mapSel.size() > 0) {
            tv_batch_title.setText("已选定" + mapSel.size() + "首");
        } else {
            tv_batch_title.setText("批量操作");
        }
        if (onBatchSelectLisenter != null) {
            onBatchSelectLisenter.onSelect(mapSel.size(), downloadedList.size());
        }
    }

    @Override
    public void onClick(View v) {
        int _id = v.getId();
        if (_id == R.id.tv_batch_left) {
            if (mapSel.size() == downloadedList.size()) {
                mapSel.clear();
            } else {
                // 全选
                for (TasksManagerModel tasksManagerModel : downloadedList) {
                    mapSel.put(tasksManagerModel.getAudioId(), tasksManagerModel);
                }
            }
            taskItemAdapter.notifyDataSetChanged();
            refreshSelect();
        } else if (_id == R.id.tv_batch_right) {
            getActivity().finish();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        TasksManagerModel tasksManagerModel = taskItemAdapter.getItem(position);
        if (mapSel.containsKey(tasksManagerModel.getAudioId())) {
            mapSel.remove(tasksManagerModel.getAudioId());
        } else {
            mapSel.put(tasksManagerModel.getAudioId(), tasksManagerModel);
        }
        taskItemAdapter.notifyItemChanged(position);
        refreshSelect();
    }

    OnBatchSelectLisenter onBatchSelectLisenter;

    public void setOnBatchSelectLisenter(OnBatchSelectLisenter onBatchSelectLisenter) {
        this.onBatchSelectLisenter = onBatchSelectLisenter;
    }

    public interface OnBatchSelectLisenter {
        void onSelect(int count, int total);
    }


}
